const mongoose = require('mongoose');
var express = require('express')
var doTitles = express()

const {createResponse}= require("../../util/createResponse/index");
const {businessCodes}= require("../../code/businessCodes/index");
const {doTitlesService } = require('../../service/impl/doTitlesService/index');


doTitles.post('/post/doTitles/add', async (req,res) => {
  let result
  console.log(req.body,555)
  try {
    let news  = req.body.news
    result =  await doTitlesService.getIns().create(news)
  } catch (error) {
    console.log(error);
    createResponse(res,500,businessCodes.b2)
    return
  }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  doTitles.post('/post/doTitles/delete', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      result =  await doTitlesService.getIns().findByIdAndDelete(id)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  doTitles.post('/post/doTitles/update', async (req,res) => {
    let result
    try {
      let  id  = req.body.conditions.id
      let updates = req.body.updates
      result =  await doTitlesService.getIns().findByIdAndUpdate(id,updates)
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  doTitles.post('/post/doTitles/query', async (req,res) => {
    let result
    try {
      result =  await doTitlesService.getIns().find()
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })


  // 查询当天的做题记录
  doTitles.post('/post/doTitles/queryLogsToday', async (req,res) => {
    let result
      // 使用新Date对象，当天日期的0点和24点，作为日期筛选
      console.log(req.body,222);

    try {
      const { userid, professionid } = req.body.conditions;
      let now = new Date();
      let startOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate());
      let endOfDay = new Date(now.getFullYear(), now.getMonth(), now.getDate()+1);
    
      result =  await doTitlesService.getIns().find(
        {
          userid:userid,
          professionid: professionid,
          'logs.time': { $gte: startOfDay, $lt: endOfDay}
        }
      )

      console.log(result,555);
    } catch (error) {
      console.log(error);
      createResponse(res,500,businessCodes.b2)
      return
    }
    createResponse(res,200,businessCodes.b1,result)
  })
  
  
  module.exports= doTitles

  /* 
  router.post('/post/doTitle/add',
  (req,res) => {
    controller.doTitleController.getSigleIns().create(req,res)
  }
)

router.post('/post/doTitle/delete',
  (req,res) => {
    controller.doTitleController.getSigleIns().findByIdAndRemove(req,res)
  }
)

router.post('/post/doTitle/update',
  (req,res) => {
    controller.doTitleController.getSigleIns().findByIdAndUpdate(req,res)
  }
)

router.post('/post/doTitle/query',
  (req,res) => {
    controller.doTitleController.getSigleIns().queryAll(req,res)
  }
)

// C01--用户提交做题结果
router.post('/post/doTitle/update/adddolog',
  (req,res) => {
    controller.doTitleController.getSigleIns().findByObjAndUpdate(req,res)
  }
)

// 根据用户id和题目id,获取以往的做题结果记录
router.post('/post/doTitle/query/userlog',
  (req,res) => {
    controller.doTitleController.getSigleIns().findByObj(req,res)
  }
)

  
  */